package org.example.activiti.controller;


import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <a href="https://blog.csdn.net/wenxingchen/article/details/119670898">...</a>
 */
@RestController
@Slf4j
public class TestController {
    @Autowired
    private RepositoryService repositoryService;
 
    @Autowired
    private RuntimeService runtimeService;
 
    @Autowired
    private TaskService taskService;
 
    /**
     * 项目启动部署请假流程引擎。
     */
    @PostConstruct
    public void prepare() {
        // 创建一个部署对象
//        Deployment deployment = repositoryService.createDeployment()
//                .name("请假流程plus")
//                .addClasspathResource("processes/demo.bpmn20.xml")
//                .deploy();
//        log.info("部署ID：" + deployment.getId());
//        log.info("部署名称：" + deployment.getName());
    }
 

    @GetMapping("startProcess")
    public void startProcess(@RequestParam String student, @RequestParam String teacher) {
        String processDefinitionKey = "demo";
        Map<String, Object> map = new HashMap<>();
 
        //使用UEL 表达式设置
 
        // 学生填写申请单    Assignee：${student}
        map.put("student", student);
 
        // 班主任审批    Assignee：${teacher}
        map.put("teacher", teacher);
 
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(processDefinitionKey, map);
 
        log.info("流程实例ID:" + instance.getId());
        log.info("流程定义ID:" + instance.getProcessDefinitionId());
    }
 

    @GetMapping("searchTask")
    public List<String> searchTask() {
        //流程启动后，各各任务的负责人就可以查询自己当前需要处理的任务，查询出来的任务都是该用户的待办任务。
        List<Task> list = taskService.createTaskQuery()
                //流程实例key
                .processDefinitionKey("demo")
                //查询谁的任务
                //.taskAssignee("")
                .list();
        List<String> idList = new ArrayList<String>();
 
        for (Task task : list) {
            idList.add(task.getId());
            log.info("任务ID:" + task.getId());
            log.info("任务名称:" + task.getName());
            log.info("任务的创建时间:" + task.getCreateTime());
            log.info("任务的办理人:" + task.getAssignee());
            log.info("流程实例ID：" + task.getProcessInstanceId());
            log.info("执行对象ID:" + task.getExecutionId());
            log.info("流程定义ID:" + task.getProcessDefinitionId());
        }
        return idList;
 
    }
 

    @GetMapping("disposeTask")
    public void disposeTask(@RequestParam List<String> list) {
        for (String id : list) {
            // 任务id
            taskService.complete(id);
            System.out.println("处理任务id：" + id);
        }
    }
}